<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	/**
	 * @author Garry Willits
	 * @copyright 
	 * @date 10th May 2011
	 */
class Gallery  extends CI_Controller{
	
	const EDIT = 0;
	
	const SLIDE = 1;
	
	const VIEW = 2;
	
	const SINGLE = 3;
	
	const ROW = 4;
	
	const MULTI = 5;
	
	const IMG_PER_PAGE =10;
	
	public $drilldownpath = "site/drillDown/";
	
	public $editpath = "admin/editImage/";
	
	public $mode;
	
	public $display_rows;
	
	public $maxwidth = 90;
	/*
	 * doesn't mean is bust means is doing something
	 */
	public $working = true;
	
	protected $dir_assets = array();
	public function __construct( $m,$s=NULL,$maxwidth=null,$category="default" )
	{   
		// validate input
		if( $m != Gallery::EDIT && $m != Gallery::SLIDE && $m != Gallery::VIEW && $s != Gallery::SINGLE && $s !=Gallery::ROW && $s != Gallery::MULTI){
			throw new BadMethodCallException("Please provide the correct mode constant for the Gallery class to initialise", 500 );
		} 
		$this->mode = $m;
		$this->display_rows = $s;
		if( isset($maxwidth) )$this->maxwidth = $s;
		parent::__construct();
		$this->load->database();
		$this->display = new GalleryDisplay();
		$this->display->id = "gallery";
		$this->display->class = "gallery";
		$this->load->database();
		$this->load->helper(array('form', 'url','path','file'));
		$this->display->scripts = 'jquery.js';
		$this->display->scripts = 'jquery-ui.js';
		$this->display->scripts = 'uiinit.js';
		$this->display->scripts = 'jquery.lightbox-0.5.js';
		$this->display->styles = 'ui';
		$this->display->styles = 'themes/smoothness/jquery.ui.all';
		$this->display->styles = 'lightbox';
		$this->display->styles = 'common';
		$this->initGallery();
		$this->display->finalize();
	}
	/**
	 * 
	 * Spits the images in the requested gallery into the $dir_assets array
	 */
	protected function initGallery(){
		$displaying = self::IMG_PER_PAGE;
		$cur = $this->uri->segment(6, 0);
		$this->load->model('GalleryModel','gallerymodel');
		$category_id = $this->uri->segment(4, 0);
		$query = $this->gallerymodel->getCategory($category_id);
		$cat_num = $this->gallerymodel->countCategory();
		
		if($cat_num > 1){
			if( $category_id < $cat_num - 1 ){
				$this->display->next_cat = ($category_id + 1)."/page/0";
			}
			$this->display->previous_cat = ($category_id - 1)."/page/0";
		}
		else {
			$this->display->previous_cat = -1;
		}
		$this->display->gallery_title = $query->name;
		$categoryID = $query->id;
		$query =$this->gallerymodel->getImages($query->id,$displaying,$cur);
		// got category
		$imgs = array();
		switch ($this->mode){
			case Gallery::MULTI;
			break;
			default:
			$renderer = new MultipleImageGallery();
			foreach ( $query as $dat ){
				$img =new Image($dat->location, (int)$this->maxwidth,$dat->width,$dat->height);
				$img->finalize();
				$wrapper = new EditableImageWrapper($dat->title);
				$wrapper->addChild($img);
				$wrapper->title = $dat->title;
				$wrapper->preview = $dat->location;
				$wrapper->id = "img$dat->id";
				$wrapper->editpath = site_url($this->editpath.$dat->id."/".$category_id."/".$cur);
				$wrapper->finalize();
				$renderer->addChild($wrapper);
				$renderer->addJSInit(""); 
				}
			$this->load->library('pagination');
			$config['base_url'] = site_url("admin/images/catID/$category_id/page");
			$config['enable_query_strings'] = TRUE;
			$config['total_rows'] = $this->gallerymodel->countImagesInCategory($categoryID);
			$config['cur_page'] = $cur;
			$config['per_page'] = Gallery::IMG_PER_PAGE; 
			$this->pagination->initialize($config); 
			$renderer->pagination = $this->pagination->create_links();
			$renderer->finalize();
			$this->display->addJSInit("
			$(function() {
			$('div.framesection img').verticalAlign();
			});
			"); 
			$this->display->addChild($renderer,'gallery_item_template'); 
		}
	}
	protected function generate(){
		
	}
}